import Vue from 'vue';
import Router from 'vue-router';

// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

Vue.use(Router);

const routes = [
  {
    path: '/',
    name: 'login',
    component: () => import('./view/login')
  },
  {
    path: '/frame',
    name: 'frame',
    component: () => import('./view/frame'),
    children: [
        {
          path: '/',
          redirect: '/work'
        },
        {
          path: '/carUsage',
          name: 'carUsage',
          component: () => import('./view/carUsage'),
          meta: {
            title: '企业用车'
          }
        },
        {
          path: '/home',
          name: 'home',
          component: () => import('./view/home'),
          meta: {
            title: '首页'
          }
        },
        {
          path: '/home/article',
          name: 'article',
          component: () => import('./view/home/article'),
          meta: {
            title: '资讯详情'
          }
        },
        {
          path: '/login',
          name: 'login',
          component: () => import('./view/login'),
          meta: {
            title: '登录'
          }
        },
        {
          path: '/personalInfo',
          name: 'personalInfo',
          component: () => import('./view/personalInfo'),
          meta: {
            title: '个人信息'
          }
        },
        {
          path: '/work',
          name: 'work',
          component: () => import('./view/work'),
          meta: {
            title: '我的工作'
          }
        },
        {
          path: '/work/workInfo',
          name: 'workInfo',
          component: () => import('./view/work/workInfo'),
          meta: {
            title: '工作详情'
          }
        },
        {
          path: '/work/examInfo',
          name: 'examInfo',
          component: () => import('./view/work/examInfo'),
          meta: {
            title: '考试详情'
          }
        },
        {
          path: '/personalInfo/idcard',
          name: 'idcard',
          component: () => import('./view/personalInfo/idcard'),
          meta: {
            title: '实名信息'
          }
        },
        {
          path: '/personalInfo/drivingLicense',
          name: 'drivingLicense',
          component: () => import('./view/personalInfo/drivingLicense'),
          meta: {
            title: '驾驶信息'
          }
        },
        {
          path: '/personalInfo/account',
          name: 'account',
          component: () => import('./view/personalInfo/account'),
          meta: {
            title: '账号与安全'
          }
        },
        {
          path: '/personalInfo/account/changePassword',
          name: 'changePassword',
          component: () => import('./view/personalInfo/account/changePassword'),
          meta: {
            title: '修改密码'
          }
        },
        {
          path: '/personalInfo/account/valPhone',
          name: 'valPhone',
          component: () => import('./view/personalInfo/account/valPhone'),
          meta: {
            title: '验证手机'
          }
        },
        {
          path: '/carUsage/usageLog',
          name: 'usageLog',
          component: () => import('./view/carUsage/usageLog'),
          meta: {
            title: '用车记录'
          }
        },
        {
          path: '/carUsage/usageLog/logDetail',
          name: 'logDetail',
          component: () => import('./view/carUsage/usageLog/logDetail'),
          meta: {
            title: '用车记录详情'
          }
        },
        {
          path: '/carUsage/carManage',
          name: 'carManage',
          component: () => import('./view/carUsage/carManage'),
          meta: {
            title: '车辆管理'
          }
        },
        {
          path: '/carUsage/reviewViolations',
          name: 'reviewViolations',
          component: () => import('./view/carUsage/reviewViolations'),
          meta: {
            title: '违规复核'
          }
        },
        {
          path: '/carUsage/usageStandard',
          name: 'usageStandard',
          component: () => import('./view/carUsage/usageStandard'),
          meta: {
            title: '用车规范'
          }
        },
        {
          path: '/carUsage/reviewViolations/reviewDetail',
          name: 'reviewDetail',
          component: () => import('./view/carUsage/reviewViolations/reviewDetail'),
          meta: {
            title: '违规复核详情'
          }
        },
        {
          path: '/carUsage/carManage/addCar',
          name: 'addCar',
          component: () => import('./view/carUsage/carManage/addCar'),
          meta: {
            title: '新增车辆'
          }
        },
        {
          path: '/carUsage/carManage/carDetail',
          name: 'carDetail',
          component: () => import('./view/carUsage/carManage/carDetail'),
          meta: {
            title: '车辆详情'
          }
        },
        {
          path: '/carUsage/carManage/editCar',
          name: 'editCar',
          component: () => import('./view/carUsage/carManage/editCar'),
          meta: {
            title: '编辑车辆'
          }
        },
        {
          path: '/carUsage/carManage/handCar',
          name: 'handCar',
          component: () => import('./view/carUsage/carManage/handCar'),
          meta: {
            title: '车辆处置'
          }
        },
        {
          path: '/carUsage/carManage/repairCar',
          name: 'repairCar',
          component: () => import('./view/carUsage/carManage/repairCar'),
          meta: {
            title: '申请设备维护'
          }
        },
        {
          path: '/carUsage/regCar',
          name: 'regCar',
          component: () => import('./view/carUsage/regCar'),
          meta: {
            title: '用车申请'
          }
        },
        {
          path: '/carUsage/flowCar',
          name: 'flowCar',
          component: () => import('./view/carUsage/flowCar'),
          meta: {
            title: '用车流程提交'
          }
        },
        {
          path: '/forgetPassword',
          name: 'forgetPassword',
          component: () => import('./view/login/forgetPassword'),
          meta: {
            title: '密码恢复'
          }
        }
    ]
  }

];

// add route path
routes.forEach(route => {
  route.path = route.path || '/' + (route.name || '');
});

const router = new Router({ routes });

router.beforeEach((to, from, next) => {
  const title = to.meta && to.meta.title;
  if (title) {
    document.title = title;
  }
  next();
});

export {
  router
};